from django.test import TestCase
from django.test.client import Client

from mdcssport.mdcsapp.models import PlayerUserProfile 

"""
Testira stranu za dodavanje poena igracima
"""
class AddMatchTestCase(TestCase):
    
    def setUp(self):
        pass

    def runTest(self):
        self.runTestGet()
        self.runTestChessPost()
        self.runTestTableFootballPost()

    def runTestGet(self):
        c = Client()
        c.login(username='kokan', password='123')
        
        response = c.get("/mdcs/add_match/")
        self.failIf(response == None)
        self.failIf(response.context == None)
        self.failUnlessEqual(len(response.context), 6)
        
        self.failIf(response.context[0]["form"] == None)

    def runTestChessPost(self):
        c = Client()
        c.login(username='kokan', password='123')
        
        response = c.post("/mdcs/add_match/",
            {'home': '6', 'away': '7', 'result': '1:0', 'game_type': '1', 'date_played': '2013-08-13 13:00'})
        self.failIf(response == None)
        self.failUnlessEqual(response.status_code, 302)
        # TODO: kako da proverim gde redirektuje
        
        profiles = PlayerUserProfile.objects.all().order_by('-chess_points')
        self.failIf(profiles == None)
        self.failUnlessEqual(len(profiles), 12)
            
        self.failUnlessEqual(profiles[0].chess_points, 1217)
        self.failUnlessEqual(profiles[1].chess_points, 1216)
        self.failUnlessEqual(profiles[2].chess_points, 1200)
        self.failUnlessEqual(profiles[3].chess_points, 1200)
        self.failUnlessEqual(profiles[4].chess_points, 1200)
        self.failUnlessEqual(profiles[5].chess_points, 1200)
        self.failUnlessEqual(profiles[6].chess_points, 1200)
        self.failUnlessEqual(profiles[7].chess_points, 1200)
        self.failUnlessEqual(profiles[8].chess_points, 1200)
        self.failUnlessEqual(profiles[9].chess_points, 1199)
        self.failUnlessEqual(profiles[10].chess_points, 1184)
        self.failUnlessEqual(profiles[11].chess_points, 1184)

    def runTestTableFootballPost(self):
        c = Client()
        c.login(username='kokan', password='123')
        
        response = c.post("/mdcs/add_match/",
            {'home': ['5', '6'], 'away': ['7', '8'], 'result': '5:3', 'game_type': '2', 'date_played': '2013-08-13 14:00'})
        self.failIf(response == None)
        self.failUnlessEqual(response.status_code, 302)
        # TODO: kako da proverim gde redirektuje
        
        profiles = PlayerUserProfile.objects.all().order_by('-tf_points', 'id')
        self.failIf(profiles == None)
        self.failUnlessEqual(len(profiles), 12)
        
        self.failUnlessEqual(profiles[0].id, 5) # atomic
        self.failUnlessEqual(profiles[0].tf_points, 1266)
        self.failUnlessEqual(profiles[1].id, 6) # milaki
        self.failUnlessEqual(profiles[1].tf_points, 1266)
        self.failUnlessEqual(profiles[2].id, 9) # milann
        self.failUnlessEqual(profiles[2].tf_points, 1215)
        self.failUnlessEqual(profiles[3].id, 10) # dragant
        self.failUnlessEqual(profiles[3].tf_points, 1215)
        self.failUnlessEqual(profiles[4].id, 2) # kokan
        self.failUnlessEqual(profiles[4].tf_points, 1200)
        self.failUnlessEqual(profiles[5].id, 3) # satom
        self.failUnlessEqual(profiles[5].tf_points, 1200)
        self.failUnlessEqual(profiles[6].id, 4) # sttara
        self.failUnlessEqual(profiles[6].tf_points, 1200)
        self.failUnlessEqual(profiles[7].id, 11) # migrbi
        self.failUnlessEqual(profiles[7].tf_points, 1200)
        self.failUnlessEqual(profiles[8].id, 12) # milostod
        self.failUnlessEqual(profiles[8].tf_points, 1200)
        self.failUnlessEqual(profiles[9].id, 13) # vvasic
        self.failUnlessEqual(profiles[9].tf_points, 1200)
        self.failUnlessEqual(profiles[10].id, 7) # mazi
        self.failUnlessEqual(profiles[10].tf_points, 1119)
        self.failUnlessEqual(profiles[11].id, 8) # milaki
        self.failUnlessEqual(profiles[11].tf_points, 1119)